package zjs.dc.controller;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

/**
 * @Class:
 * @Author: xyl
 * @Description: 针对企查查 t_shixin 表和中数 t_dis_sxbzxr_new 表进行数据映射开发
 * @Date: 2021/12/16 15:47
 */
public class TShixin {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder().appName("t_shixin").enableHiveSupport().getOrCreate();
        spark.sparkContext().setLogLevel("ERROR");
        Dataset<Row> zs_data = spark.sql("select * from a_dc_ep_ods.zs_t_dis_sxbzxr_new");
        long zs_count = zs_data.count();
        if(zs_count>0){

            /**
             * t_shixin 失信信息
             */
            //获取中数删除数据表中的数据 --- 中数删除逻辑：此表需要使用“案号”来删除
            Dataset<Row> delete_data = spark.sql("select \n" +
                    "   a.id,a.key_no,a.company_id,a.company_name,a.sx_id,a.name,a.lian_date,a.an_no,a.org_no,\n" +
                    "   a.owner_name,a.execute_gov,a.province,a.execute_unite,a.yiwu,a.execute_status,a.actionremark,a.public_date,a.age, \n" +
                    "   a.sexy,a.execute_no,a.performedpart,a.unperformpart,a.is_valid,a.org_type,'' as EXITDATE,b.jobid,'-1' as isadd  \n" +
                    "from (select * from a_dc_ep_dwi.t_shixin where an_no is not null and an_no !='')a \n" +
                    "inner join a_dc_ep_ods.zs_t_dis_sxbzxr_new_del b  \n" +
                    "on upper(trim(translate(translate(a.an_no,\"（\",\"(\"),\"）\",\")\"))) = upper(trim(translate(translate(b.CASECODE,\"（\",\"(\"),\"）\",\")\"))) \n" +
                    "where b.CASECODE is not null and b.CASECODE !=''  \n");
            delete_data.createOrReplaceTempView("tmp_t_shixin");
            spark.sql("insert overwrite table a_dc_ep_incr.t_shixin select * from tmp_t_shixin");

            //更新数据--“案号”关联
            Dataset<Row> insert_data = spark.sql("SELECT " +
                    "       md5(concat_ws('',b.company_id,translate(translate(a.CASECODE,\"(\",\"（\"),\")\",\"）\"))),\n" +
                    "       b.key_no,\n" +
                    "       b.company_id,\n" +
                    "       b.company_name,\n" +
                    "       a.RECORD_ID,\n" +
                    "       a.INAMECLEAN,\n" +
                    "       a.REGDATECLEAN,\n" +
                    "       a.CASECODE,\n" +
                    "       a.CARDNUM,\n" +
                    "       a.BUSINESSENTITY,\n" +
                    "       a.COURTNAME,\n" +
                    "       a.AREANAMECLEAN,\n" +
                    "       a.GISTUNIT,\n" +
                    "       a.DUTY,\n" +
                    "       a.PERFORMANCE,\n" +
                    "       a.DISRUPTTYPENAME,\n" +
                    "       a.PUBLISHDATECLEAN,\n" +
                    "       \"\",\n" +
                    "       \"\",\n" +
                    "       a.GISTID,\n" +
                    "       a.PERFORMEDPART,\n" +
                    "       a.UNPERFORMPART,\n" +
                    "       if(substr(cast(current_timestamp() as string),1,10)>a.EXITDATE and length(a.EXITDATE) == 10,0,if(length(a.EXITDATE) != 10,'',1)),\n" +
                    "       if(trim(a.type)=\"自然人\",'1','2'),\n" +
                    " a.EXITDATE, " +
                    "       a.JOBID,\n" +
                    "       \"0\" as isadd  " +
                    "FROM\n" +
                    " (SELECT * FROM a_dc_ep_ods.zs_t_dis_sxbzxr_new WHERE CREDITCODE IS NOT NULL AND CREDITCODE !='' AND CASECODE IS NOT NULL AND CASECODE !='') a\n" +
                    "inner join \n" +
                    "  (SELECT * FROM a_dc_ep_ods.t_eci_company WHERE credit_code IS NOT NULL AND credit_code!='') b \n" +
                    "ON upper(trim(a.CREDITCODE)) = upper(trim(b.credit_code))  \n");
            insert_data.createOrReplaceTempView("tmp_t_shixin");
            spark.sql("insert into table a_dc_ep_incr.t_shixin select * from tmp_t_shixin");
        }else {
            spark.sql("TRUNCATE TABLE a_dc_ep_incr.t_shixin");
            System.out.println("中数本期数据涉及该表的数据为空。。。。。。");
        }

        spark.stop();
    }
}
